package com.xunyin.modules.couponnew.service.mapper;


import com.xunyin.common.mapper.CoreMapper;
import com.xunyin.modules.couponnew.bean.CouponNumBean;
import com.xunyin.modules.couponnew.domain.MtUserCoupon;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 会员卡券表 Mapper 接口
 * <p>
 * Created by xunyinyunshang <3594419785@qq.com>
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Repository
public interface MtUserCouponMapper extends CoreMapper<MtUserCoupon> {


    Long getSendNum(@Param("couponId") Integer couponId);

    CouponNumBean getPeopleNumByCouponId(@Param("couponId") Integer couponId);

    @Select("<script>" +
            " select * from mt_user_coupon t where t.USER_ID = #{userId} " +
            " <if test =\"refGive !=null\"> and ref_give = #{refGive} </if> " +
            " AND t.STATUS in " +
            " <foreach collection=\"statusList\" item=\"status\"  open=\"(\" separator=\",\" close=\")\"> " +
            "  #{status} " +
            " </foreach> " +
            "</script>")
    List<MtUserCoupon> getUserCouponList(@Param("userId") Integer userId, @Param("statusList") List<String> statusList,@Param("refGive") String refGive);


    @Select("SELECT count(0) from mt_user_coupon o " +
            " where o.type in ('H','N') and o.user_id = #{userId} ")
    Integer countUserCoupon(@Param("userId") Integer userId);

    @Select("<script> select IFNULL(sum(amount),0) from mt_user_coupon " +
            " where is_del=0 and status not in ('D') " +
            " <if test =\"storeId !=null\"> and store_id = #{storeId} </if> " +
            " <if test =\"storeIds !=null\"> and store_id in (#{storeIds}) </if> " +
            " <if test =\"type !=null\"> and type = #{type} </if> " +
            " <if test =\"status !=null\"> and status = #{status} </if> " +
            " <if test =\"mobile !=null\"> and mobile = #{mobile} </if> " +
            "</script>")
    Double sumUserCouponMoney(@Param("storeId") Integer storeId,
                              @Param("storeIds") String storeIds,
                              @Param("type") String type,
                              @Param("status") String status,
                              @Param("mobile") String mobile);

    List<MtUserCoupon> getUserCouponListByCouponId(@Param("userId") Integer userId, @Param("couponId") Integer couponId, @Param("statusList") List<String> statusList);

    @Select("SELECT * FROM mt_user_coupon t WHERE t.CODE = #{code} " +
            " ORDER BY t.ID DESC")
    MtUserCoupon findByCode(@Param("code") String code);

    int removeUserCoupon(@Param("uuid") String uuid, @Param("couponIds") List<Integer> couponIds, @Param("operator") String operator);

    List<MtUserCoupon> queryExpireNumByGroupId(@Param("groupId") Integer groupId);

    List<Integer> getCouponIdsByUuid(@Param("uuid") String uuid);

    List<MtUserCoupon> findUserCouponDetail(@Param("couponId") Integer couponId, @Param("userId") Integer userId);

    List<MtUserCoupon> getUserCouponListByExpireTime(@Param("userId") Integer userId, @Param("status") String status, @Param("startTime") String startTime, @Param("endTime") String endTime);

}
